package com.chain.BrotherExchange.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.widget.CheckBox;
import android.widget.TextView;

import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.UserAssets;
import com.chain.BrotherExchange.event.UserAssetsChangeEvent;
import com.chain.BrotherExchange.event.UserAssetsVisibilityEvent;
import com.chain.BrotherExchange.ui.adapter.SimpleFragmentPagerAdapter;
import com.chain.BrotherExchange.ui.core.BaseActivity;
import com.chain.BrotherExchange.ui.fragment.UserAssetsListFragment;
import com.chain.BrotherExchange.utils.ActivityBarHelper;
import com.chain.BrotherExchange.utils.Androids;
import com.chain.BrotherExchange.viewmodel.MyAssetsViewModel;
import com.chain.BrotherExchange.viewmodel.core.ViewModelProviders;
import com.example.common.widget.state.NetworkStateLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.Arrays;

import butterknife.Bind;

/**
 * 我的资产
 */
public class MyAssetsActivity extends BaseActivity {

    @Bind(R.id.tab_layout) TabLayout mTabLayout;
    @Bind(R.id.coin_assets_text) TextView mCoinAssetsText;
    @Bind(R.id.total_assets_text) TextView mTotalAssetsText;
    @Bind(R.id.coin_assets_cny_text) TextView mCoinAssetsCnyText;
    @Bind(R.id.my_assets_view_pager) ViewPager mMyAssetsViewPager;
    @Bind(R.id.total_assets_cny_text) TextView mTotalAssetsCnyText;
    @Bind(R.id.legal_tender_assets_text) TextView mLegalTenderAssetsText;
    @Bind(R.id.network_state_layout) NetworkStateLayout mNetworkStateLayout;
    @Bind(R.id.assets_visibility_check_box) CheckBox mAssetsVisibilityCheckBox;
    @Bind(R.id.legal_tender_assets_cny_text) TextView mLegalTenderAssetsCnyText;

    private MyAssetsViewModel mViewModel;
    private UserAssets.DataBean mUserAssets;

    public static void launch(Context context) {
        context.startActivity(new Intent(context, MyAssetsActivity.class));
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_my_assets;
    }

    @Override
    protected void initActions() {
        mViewModel = ViewModelProviders.of(this).get(MyAssetsViewModel.class);

        ActivityBarHelper.initial(this, R.string.title_my_assets, R.drawable.ic_detail_list, v ->
                AssetsDetailsActivity.launch(v.getContext()));

        // 用户总资产信息
        initUserAssetsInfo();

        // 用户资产列表：币币、法币
        initUserAssetsList();

        // 控制资产可见性
        controlAssetsVisibility();
    }

    @Override
    protected boolean useEventBus() {
        return true;
    }

    private void controlAssetsVisibility() {
        mAssetsVisibilityCheckBox.setChecked(Androids.LocalConfig.getUserAssetsVisibility());
        mAssetsVisibilityCheckBox.setOnCheckedChangeListener((View, isChecked) -> {
            EventBus.getDefault().post(new UserAssetsVisibilityEvent(isChecked));
            Androids.LocalConfig.setUserAssetsVisibility(isChecked);
            loadUserAssetsOntoView(isChecked);
        });
    }

    private void initUserAssetsList() {
        mTabLayout.setupWithViewPager(mMyAssetsViewPager);
        mMyAssetsViewPager.setAdapter(new SimpleFragmentPagerAdapter(
                getSupportFragmentManager(),
                new ArrayList<>(Arrays.asList(
                        UserAssetsListFragment.coinAssetsFragment(),
                        UserAssetsListFragment.legalCurrencyFragment()
                )),
                new ArrayList<>(Arrays.asList(
                        getString(R.string.title_coin_asset),
                        getString(R.string.title_french_currency_assets))))
        );
    }

    private void initUserAssetsInfo() {
        mViewModel.requestUserAsserts().observe(this, resource -> {
            Resource.load(this, resource, mNetworkStateLayout, result -> {
                mUserAssets = resource.source;
                loadUserAssetsOntoView(Androids.LocalConfig.getUserAssetsVisibility());
            });
        });
    }

    private void loadUserAssetsOntoView(boolean visibility) {
        UserAssets.DataBean userAssets;
        if (visibility) {
            userAssets = UserAssets.generateAssetsSymbol();
        } else {
            userAssets = mUserAssets;
        }

        mTotalAssetsText.setText(userAssets.getAll_balance());
        mTotalAssetsCnyText.setText(userAssets.getAll_cny());
        mCoinAssetsText.setText(userAssets.getTotal_worth_btc());
        mCoinAssetsCnyText.setText(userAssets.getTotal_worth_cny() + " CNY");
        mLegalTenderAssetsCnyText.setText(userAssets.getSum_cny2() + " CNY");
        mLegalTenderAssetsText.setText(userAssets.getSum2());
    }

    @Subscribe
    public void userAssetsChnageEvent(UserAssetsChangeEvent event) {
        initUserAssetsInfo();
    }
}
